package com.ztgf.search.api.cms;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.search.models.bo.SearchIndexBO;
import com.ztgf.search.models.bo.SearchIndexSaveBO;
import com.ztgf.search.models.dto.SearchIndexDTO;
import com.ztgf.search.models.service.ISearchIndexService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 搜索引擎索引表控制器
 *
 * @author chenyu.wang
 * @Date 2021-01-28 18:13:28
 */

@RestController
@RequestMapping("/cms/searchIndex")
@Slf4j
@Api(value = "SearchIndexController", tags = "搜索引擎索引表接口")
public class SearchIndexController {

    @DubboReference
    private ISearchIndexService searchIndexService;

    @ApiOperation(value = "添加搜索引擎索引表", notes = "添加搜索引擎索引表")
    @PostMapping(value = "/insert")
    public Result<?> insert(SearchIndexSaveBO saveBO) {
        boolean flag = searchIndexService.add(saveBO);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @ApiOperation(value = "搜索引擎索引表列表", notes = "搜索引擎索引表列表")
    @PostMapping("/queryPageList")
    public Result<Page<SearchIndexDTO>> queryPageList(SearchIndexBO bo) {
        if (bo.getId() == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        Wrapper<SearchIndexDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id", bo.getId());
        Page<SearchIndexDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = searchIndexService.selectPage(page, wrapper);
        return new Result<>(page);
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除索引", notes = "删除索引")
    public Result<?> delete(Long id) {
        boolean delete = searchIndexService.delete(id);
        return new Result<>(delete);
    }
}
